<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Template • TodoMVC</title>
		<link rel="stylesheet" href="node_modules/todomvc-common/base.css">
		<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
		<!-- CSS overrides - remove if you don't need it -->
		<link rel="stylesheet" href="css/app.css">
		<script src="bower_components/angular/angular.js"></script>
	</head>
<body ng-app="todo" ng-controller="todoController" ng-cloak="">
		<section class="todoapp">
			<header class="header">
				<h1>todos</h1>
				<form action="#/" ng-submit="addItem(newWord)">
					<input ng-model="newWord" class="new-todo" placeholder="What needs to be done?" autofocus>
				</form>
			</header>
			<!-- This section should be hidden by default and shown when there are todos -->
			<section class="main">
				<input id="toggle-all" class="toggle-all" type="checkbox" ng-click="toggleSelectAll()" ng-model="isSelectAll">
				<label for="toggle-all">Mark all as complete</label>
				<ul class="todo-list">
					<li ng-repeat="item in list | filter:filterFunc track by $index" ng-class="{completed:item.completed}">
						<div class="view">
							<input class="toggle" type="checkbox" ng-model="item.completed">
							<label>{{item.title}}</label>
							<button class="destroy" ng-click="deleteItem(item.id)"></button>
						</div>
						<input class="edit" value="Rule the web">
					</li>
					<!-- These are here just to show the structure of the list items -->
					<!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
					<!--<li class="completed">-->
						<!--<div class="view">-->
							<!--<input class="toggle" type="checkbox" checked>-->
							<!--<label>Taste JavaScript</label>-->
							<!--<button class="destroy"></button>-->
						<!--</div>-->
						<!--<input class="edit" value="Create a TodoMVC template">-->
					<!--</li>-->
					<!--<li>-->
						<!--<div class="view">-->
							<!--<input class="toggle" type="checkbox">-->
							<!--<label>Buy a unicorn</label>-->
							<!--<button class="destroy"></button>-->
						<!--</div>-->
						<!--<input class="edit" value="Rule the web">-->
					<!--</li>-->
				</ul>
			</section>
			<!-- This footer should hidden by default and shown when there are todos -->
			<footer class="footer">
				<!-- This should be `0 items left` by default -->
				<span class="todo-count"><strong>{{selectCount()}}</strong> item left</span>
				<!-- Remove this if you don't implement routing -->
				<ul class="filters">
					<li>
						<a href="#/" ng-class="{selected:status == '/'}">All</a>
					</li>
					<li>
						<a href="#/active" ng-class="{selected:status == '/active'}">Active</a>
					</li>
					<li>
						<a href="#/completed" ng-class="{selected:status == '/completed'}">Completed</a>
					</li>
				</ul>
				<!-- Hidden if no completed items are left ↓ -->
				<button class="clear-completed" ng-click="clearSelectAll()">Clear completed</button>
			</footer>
		</section>
		<footer class="info">
			<p>Double-click to edit a todo</p>
			<!-- Remove the below line ↓ -->
			<p>Template by <a href="http://sindresorhus.com">Sindre Sorhus</a></p>
			<!-- Change this out with your name and url ↓ -->
			<p>Created by <a href="http://todomvc.com">you</a></p>
			<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
		</footer>
		<!-- Scripts here. Don't remove ↓ -->
		<script src="node_modules/todomvc-common/base.js"></script>
		<script src="js/app.js"></script>
	</body>
</html>
<script>

	var app = angular.module("todo", []);
	app.controller("todoController", ["$scope", "$location", function ($scope, $location) {

		$scope.isSelectAll = false;
		$scope.status = "";
		$scope.newWord = "";

		$scope.list = [
			{
				"id": 1,
				"title": "王思聪随礼30万",
				"completed": true
			},
			{
				"id": 2,
				"title": "FF融资闹乌龙",
				"completed": false
			},
			{
				"id": 3,
				"title": "试婚一个月后分手",
				"completed": false
			},
			{
				"id": 4,
				"title": "美国酒吧发生枪击",
				"completed": true
			}
		];

		$scope.toggleSelectAll = function () {
			$scope.list.forEach(function (item, index) {
				item.completed = $scope.isSelectAll;
			});
		};

		$scope.clearSelectAll = function () {
			$scope.isSelectAll = false;
			$scope.list.forEach(function (item, index) {
				item.completed = $scope.isSelectAll;
			});
		};

		$scope.selectCount = function () {
			var i = 0;
			$scope.list.forEach(function (item, index) {
				if (item.completed) {
					i++;
				}
			});
			return i;
		};

		$scope.deleteItem = function (id) {

			var tempList = $scope.list;
			tempList.forEach(function (item, index) {
				if (item.id == id) {
					$scope.list.splice(index, 1);
					return;
				}
			});

		};

		$scope.filterFunc = function (item) {

			var filterString = $location.$$hash;
			$scope.status = filterString;

			if (filterString == "/completed") {
				return item.completed == true;
			}else if (filterString == "/active") {
				return item.completed == false;
			}else {
				return true;
			}
		};

		$scope.addItem = function (word) {
			var item = $scope.list[$scope.list.length - 1];
			var id = 1;
			if (item) {
				id = item.id + 1;
			};

			var newItem = {
				"id": 1,
				"title": word,
				"completed": false
			};
			$scope.list[$scope.list.length] = newItem;
			$scope.newWord = "";
		}

	}]);

</script>
